package cn.dankal.flyaudio.login;

import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.PhoneOrEmailIsExistBean;
import cn.dankal.basiclib.protocol.LoginProtocol;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.Constants;
import cn.dankal.flyaudio.R;

@Route(path = LoginProtocol.FORGET_PASSWD)
public class ForgetPasswdActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText mEtPhone;

    @Autowired(name = Constants.TYPE)
    String type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_passwd;
    }

    @Override
    protected void initComponents() {
        ARouter.getInstance().inject(this);
        addSingleTitleBar(type);
    }


    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        String phone = mEtPhone.getText().toString().trim();

        if (!StringUtil.isValid(phone)) {
            ToastUtils.showShort("请输入手机号码");
            return;
        }

        if (!StringUtil.checkPhone(phone)) {
            ToastUtils.showShort("手机号码格式错误");
            return;
        }
        int checkType = 0;
        // 0手机 1邮箱
        UserServiceFactory.phoneOrEmailIsExist(phone, checkType)
                .subscribe(new AbstractDialogSubscriber<PhoneOrEmailIsExistBean>(this) {
                    @Override
                    public void onNext(PhoneOrEmailIsExistBean phoneOrEmailIsExistBean) {
                        int reason = Integer.valueOf(phoneOrEmailIsExistBean.getReason());
                        switch (reason) {
                            case Constants.PhoneExistStatus.isExist:
                                // 如果是忘记密码
                                if (type.equals(Constants.TYPE_FORGET_PASS)) {
                                    // 跳转到修改密码页面
                                    ARouter.getInstance().build(LoginProtocol.GETCODE)
                                            .withString(SetLoginPasswdActivity.MOBLIE, mEtPhone.getText().toString())
                                            .withString(Constants.TYPE, type)
                                            .navigation();
                                    finish();
                                }
                                if(type.equals(Constants.TYPE_REGISTER)){
                                    ToastUtils.showShort("该手机号码已被注册");
                                }
                                break;

                            case Constants.PhoneExistStatus.isNotExist:
                                if (type.equals(Constants.TYPE_REGISTER)) {
                                    // 跳转到注册页面
                                    ARouter.getInstance().build(LoginProtocol.GETCODE)
                                            .withString(SetLoginPasswdActivity.MOBLIE, mEtPhone.getText().toString())
                                            .withString(Constants.TYPE, type)
                                            .navigation();
                                    finish();
                                }
                                // 如果是忘记密码界面
                                if(type.equals(Constants.TYPE_FORGET_PASS)){
                                    ToastUtils.showShort("该手机号码未注册");
                                }
                                break;
                        }
                    }
                });
        return;

    }
}
